<?php

namespace App\Http\Controllers;

use App\Exceptions\ErrorCodeException;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\CoolNumberShoppingMall\CoolNumberServices;
use App\Http\Services\CoolNumberShoppingMall\ShoppingMallGoodsServices;
use App\Http\Services\CoolNumberShoppingMall\ShoppingMallOrderServices;
use Illuminate\Http\Request;

class CoolNumberController extends BaseController
{
    /**
     * @var CoolNumberServices
     */
    private $service;

    /**
     * @var ShoppingMallGoodsServices
     */
    private $serviceGoods;

    /**
     * @var ShoppingMallOrderServices
     */
    private $serviceOrder;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->service = new CoolNumberServices();
        $this->serviceGoods = new ShoppingMallGoodsServices();
        $this->serviceOrder = new ShoppingMallOrderServices();
    }

    /**
     * 获取基本信息
     *
     * Created at 2021/12/7 17:06
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @throws ErrorCodeException
     */
    public function getBasicInfo()
    {
        $loginUserId = $this->getLoginUserId();
        if (!$loginUserId) {
            throw new ErrorCodeException(Hint::LOGIN_OUT);
        }

        OutJson::outJsonResult(Hint::SUCCESS, $this->service->getBasicInfo($loginUserId));
    }

    /**
     * 靓号列表
     *
     * Created at 2021/12/7 18:34
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function listGoods(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceGoods->listGoods($request));
    }

    /**
     * 购买或赠送
     *
     * Created at 2021/12/8 09:59
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws ErrorCodeException
     * @throws \App\Exceptions\PublicException
     */
    public function buy(Request $request)
    {
        $loginUserId = $this->getLoginUserId();
        if (!$loginUserId) {
            throw new ErrorCodeException(Hint::LOGIN_OUT);
        }

        $goodsId = intval($request->input('id', 0));// 商品id
        if (!$goodsId) {
            throw new ErrorCodeException(Hint::PARAM_ERROR);
        }

        $toUserId = intval($request->input('to_user_id', 0));// 赠送给哪个用户
        $this->serviceGoods->buy($loginUserId, $goodsId, $toUserId);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 使用靓号
     *
     * Created at 2021/12/8 16:38
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws ErrorCodeException
     * @throws \App\Exceptions\PublicException
     */
    public function useCoolNumber(Request $request)
    {
        $orderId = intval($request->input('id', 0));// 订单id
        if (!$orderId) {
            throw new ErrorCodeException(Hint::PARAM_ERROR);
        }

        $this->serviceOrder->useCoolNumber($orderId);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 赠送靓号(道具背包)
     *
     * Created at 2021/12/8 17:19
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws ErrorCodeException
     * @throws \App\Exceptions\PublicException
     */
    public function presentCoolNumber(Request $request)
    {
        $orderId = intval($request->input('id', 0));
        $toUserId = intval($request->input('present_user_id', 0));
        if (!$orderId || !$toUserId) {
            throw new ErrorCodeException(Hint::PARAM_ERROR);
        }

        $this->serviceOrder->presentCoolNumber($orderId, $toUserId);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 靓号过期延时回调
     *
     * Created at 2021/12/8 19:28
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function delayedCallback(Request $request)
    {
        $buyUserId = intval($request->input('user_id'));
        $uuid = $request->input('uuid', '');
        $goodsId = intval($request->input('goods_id'));
        $this->serviceOrder->delayedCallback($uuid);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * Created at 2021/12/23 20:33
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function beOverdueNotice(Request $request)
    {
        $uuid = $request->input('uuid', '');
        $type = intval($request->input('type'));
        $this->serviceOrder->beOverdueNotice($uuid, $type);
        OutJson::outJsonResult(Hint::SUCCESS);
    }
}